
import api from '../../../utils/api.js'
import { config } from '../../../utils/config'
import utils from "../../../utils/util"

var app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		orderType: false, //选择订单类型
		imgUrl: config.imgUrl,
		orderList: [],
		total: 0,
		pageNum: 1, //
		page: 1,
		payBtn: true,
		inputDelete: false,
		inputValue: '',
		type: 0,
		isfind: false, //是否查找过 缺省状态判断
		orderItem: [
			{ type: 0, typeName: '售后申请' },
			{ type: 1, typeName: '进行中' },
			{ type: 2, typeName: '申请记录' },
			// {type : 3, typeName : '待评***价'}
		],
		orderList: [],
		saleList: [],
		orderClass: [
			{ name: '地标商城', path: '/pages/refund/landmarkList/landmarkList' }
		],
		refund_status: [
			{ statusId: 1, refund_text: '已提交申请', status: '请耐心等待客服审核' },
			{ statusId: 2, refund_text: '审核通过', status: '请耐心等待退款' },
			{ statusId: 3, refund_text: '商家驳回', status: '感谢您对味歌的支持' },
			{ statusId: 4, refund_text: '退款成功', status: '感谢您对味歌的支持' },
			{ statusId: 5, refund_text: '用户取消售后', status: '感谢您对味歌的支持' },
			{ statusId: 6, refund_text: '用户已寄回', status: '请耐心等待退款' },
			{ statusId: 10, refund_text: '服务关闭', status: '感谢您对味歌的支持' },
		]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let type = options.type
		if (type) {
			this.setData({
				type
			})
			let orderItem = this.data.orderItem;
			let typeObj = orderItem.find(item => {
				return item.type == type
			})
			wx.setNavigationBarTitle({
				title: typeObj.typeName,
			})
		}
		this.inteAftersaleOrderList();
	},
	// 选择订单类型
	orderType() {
		this.setData({
			orderType: !this.data.orderType
		})
	},

	// 可售后订单列表   type  == 0
	inteAftersaleOrderList() {
		let that = this;
		let data = {
			page: this.data.pageNum
		}
		utils.getInfo(api.inteAftersaleOrderList, "GET", data).then(result => {

			let refund_status = this.data.refund_status;
			result.data.forEach(item => {
				for (let index = 0; index < refund_status.length; index++) {
					if (refund_status[index].statusId == item.refund_status) {
						item.refund_desc = refund_status[index]['status']
					}

				}
			})

			that.setData({
				total: result.total,
				orderList: that.data.orderList.concat(result.data),
				totalPage: result.last_page,
				isfind: true
			})
		})
	},

	// 售后服务列表
	inteAftersaleList() {
		let that = this;
		let data = {
			page: this.data.page,
			type: this.data.type
		}
		utils.getInfo(api.inteAftersaleList, "GET", data).then(result => {

			let refund_status = this.data.refund_status;
			result.data.forEach(item => {
				for (let index = 0; index < refund_status.length; index++) {
					if (refund_status[index].statusId == item.refund_status) {
						item.refund_desc = refund_status[index]['status']
					}

				}
			})


			that.setData({
				saleTotal: result.total,
				saleList: that.data.saleList.concat(result.data),
				saletotalPage: result.last_page,
				isfind: true
			})
		})
	},
	refund(e) {

		let order = e.currentTarget.dataset.order;
		utils.getInfo(api.inteIsCanafter, "GET", { order_no: order }, false).then(res => {
			wx.navigateTo({
				url: `/pages/refund/pomAdd/pomAdd?order=${order}`,
			})
		})
	},



	//根据状态查询订单
	anyOrder(e) {
		let orderType = e.currentTarget.dataset.type;
		let orderItem = this.data.orderItem;
		let typeObj = orderItem.find(item => {
			return item.type == orderType
		})
		wx.setNavigationBarTitle({
			title: typeObj.typeName,
		})
		this.setData({
			type: orderType,
			pageNum: 1,
			page: 1,
			orderList: [],
			saleList: [],
			orderType: false,
			isfind: false
		})
		if (orderType == 0) {
			this.inteAftersaleOrderList();
		} else {
			this.inteAftersaleList();
		}
	},

	// 点击可售后订单详情
	orderDetail(e) {
		let order_no = e.currentTarget.dataset.order;
		wx.navigateTo({
			url: `/pages/refund/pomRefDetail/pomRefDetail?order=${order_no}`
		})
	},
	// 售后订单详情
	orderDetailHandle(e) {
		let order_no = e.currentTarget.dataset.order;
		let paymeth = e.currentTarget.dataset.paymeth;
		wx.navigateTo({
			url: `/pages/refund/pomDetail/pomDetail?order=${order_no}&paymeth=${paymeth}`
		})
	},



	//删除订单
	deleteOrder(e) {
		let that = this;
		let order_no;
		if (e && e.currentTarget.dataset.order) {
			order_no = e.currentTarget.dataset.order;
		} else {
			order_no = this.data.deleteOrder
		}
		if (this.data.deleteOrder) {
			this.delOrderApi(this.data.deleteOrder)
		} else {
			wx.showModal({
				title: '提示',
				content: '确定删除订单',
				confirmColor: '#EA5D56',
				success(res) {
					if (res.confirm) {

						that.delOrderApi(order_no)

					} else if (res.cancel) {
						return;
					}
				}
			})
		}


	},
	/*删除订单api*/
	delOrderApi(order_no) {
		let that = this
		http.request({
			url: api.integralDelOrder,
			method: "POST",
			data: {
				order_no
			},
			success: (res) => {
				console.log(res);
				if (res.code == 200) {
					// 支付成功需要修改菜篮
					// wx.showToast({
					//     title: '删除成功',
					//     icon: 'success',
					//     duration: 1500
					// })

					let orderList = that.data.orderList
					orderList.forEach((item, index) => {
						if (item.order_no == order_no) {
							orderList.splice(index, 1)
						}
					})
					that.setData({
						orderList,
						deleteOrder: ''
					})

				}
			},
			fail: (res) => {

			}
		})
	},


	/*搜索按钮*/

	searchHandle(e) {
		let inputValue = e.detail.inputValue
		this.setData({
			inputValue
		})
		wx.navigateTo({
			url: '/pages/pointsMall/searchList/searchList?keyword=' + inputValue
		})
	},



	/*切换订单分类*/
	changeOrderType(e) {
		let index = e.currentTarget.dataset.index;
		let orderClass = this.data.orderClass;
		let hrefPath = orderClass[index]
		wx.redirectTo({
			url: hrefPath.path
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},


	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function (options) {
		let that = this

		if (this.data.refresh) {

			this.setData({
				pageNum: 1,
				page: 1,
				orderList: [],
				saleList: [],
				isfind: false,
				refresh: false
			})
			let orderType = this.data.type;
			if (orderType == 0) {
				this.inteAftersaleOrderList();
			} else {
				this.inteAftersaleList();
			}
		}


	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	// /**
	//  * 页面相关事件处理函数--监听用户下拉动作
	//  */
	// onPullDownRefresh: function() {
	//     console.log('下拉刷新');
	//     if (this.data.pageNum < this.data.totalPage) {
	//         this.setData({
	//             pageNum: pageNum + 1
	//         })
	//         this.getOrderList();
	//     }
	//     wx.stopPullDownRefresh();

	// },

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		console.log('触底刷新');
		let type = this.data.type;
		if (type == 0) {
			if (this.data.pageNum < this.data.totalPage) {
				this.setData({
					pageNum: this.data.pageNum + 1
				})
				this.inteAftersaleOrderList();
			}
		} else {
			if (this.data.page < this.data.saletotalPage) {
				this.setData({
					page: this.data.page + 1
				})
				this.inteAftersaleList();
			}
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})